<template>
	<view class="pages-service">
		<fixed>
			<view style="width: 100%;height: 200rpx;">
				<image :src="backgroundImg" style="width: 100%;height: 100%;"></image>
				<view style="display: flex;left: 30rpx;position: absolute; top: 0rpx;">
					<view class="city-info" >
							<!-- #ifdef APP-PLUS -->
							<picker @change="pickerChange($event)" :value="cityIndex" :range="cityList"
								range-key="title" v-if="location.lat && location.lng">
								<view class="city-info flex-y-center">
									<view style="color: #fff;padding-top: 40rpx;">
										<block v-if="cityList&&cityList.length > 0">
											{{cityIndex === -1 ? '未知' : cityList[cityIndex].title.length > 3 ? cityList[cityIndex].title.substring(0,3) + '...':cityList[cityIndex].title}}
										</block>
										<block v-else>请选择</block>
									</view>
									<i class="iconfont iconshaixuanxia-1 ml-sm" style="font-size: 20rpx;padding-top: 40rpx;"></i>
								</view>
							</picker>
							<view @tap.stop="toOpenLocation" class="city-info flex-y-center" v-else>
								<view style="color: #fff;padding-top: 40rpx;"> 未知 </view>
								<i class="iconfont iconshaixuanxia-1 ml-sm" style="font-size: 20rpx;padding-top: 40rpx;"></i>
							</view>
										
							<!-- #endif -->
							<!-- #ifndef APP-PLUS -->
							<picker @change="pickerChange($event)" :value="cityIndex" :range="cityList"
								range-key="title">
								<view class="city-info flex-y-center">
									<view style="color: #fff;padding-top: 40rpx;">
										<block v-if="cityList&&cityList.length > 0">
											{{cityIndex === -1 ? '未知' : cityList[cityIndex].title.length > 3 ? cityList[cityIndex].title.substring(0,3) + '...':cityList[cityIndex].title}}
										</block>
										<block v-else>请选择</block>
									</view>
									<i class="iconfont iconshaixuanxia-1 ml-sm" style="font-size: 20rpx;padding-top: 40rpx;"></i>
								</view>
							</picker>
							<!-- #endif -->
						</view>
						
						
						<search @input="toSearch" type="input" :padding="30" :backgroundColor="'transparent'" :radius="0" placeholder="请输入服务名称">
						</search>
						<image @input="toSearch" :src="site_root+'static/img/search.png'" style="width: 50rpx;height: 50rpx; margin-left: 20rpx; margin-top: 35rpx;"></image>
						
				</view>
				
				<view style="width: 80%;display: flex;position: absolute; top: 120rpx;left: 10%;color: #fff;display: none;">
					<image :src="positionImg" style="width: 40rpx;height:40rpx;margin-right: 40rpx;"></image>
					<view>{{location&&location.address ? location.address : isLoad ? '定位失败' : '定位中...'}}</view>
				</view>
			</view>
				
			
		</fixed>

		<view style="width: 90%;margin-left: 5%;margin-bottom: 40rpx;" class="fill-base mt-md ml-md mr-md pd-lg radius-16" v-for="(item,index) in list.data" :key="index">
			<service-list-item :info="item" :maxWidth="'320rpx'"></service-list-item>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1">
		</abnor>
		<view class="space-footer"></view>
 
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex"
	import serviceListItem from "@/components/service-list-item.vue"
	export default {
		components: {
			serviceListItem
		},
		data() {
			return {
				options: {},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false,
				site_root:getApp().globalData.siteroot,
				backgroundImg:getApp().globalData.siteroot+'/static/img/top_bg.png',
				
				cityId:0,cityIndex:0,cityList:[]
				
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			location: state => state.user.location,
		}),
		onLoad(options) {
			let {
				title = 0
			} = options
			this.options = options
			uni.setNavigationBarTitle({
				title
			})
			this.initIndex()
			this.getCity()
		}, 
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			this.getList();
		},
		methods: {
			...mapMutations(['updateUserItem', 'updateOrderItem', 'updateTechnicianItem',
				'updateMapItem', 'updateShopstoreItem', 'updateDynamicItem'
			]),
			async initIndex(refresh = false) {
				// #ifdef H5
				if (!refresh && this.$jweixin.isWechat()) {
					await this.$jweixin.initJssdk();
					this.$jweixin.wxReady(() => {
						this.$jweixin.hideOptionMenu()
					})
				}
				// #endif
				await this.getList()
				this.$util.setNavigationBarColor({
					bg: this.primaryColor
				})
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex(true)
			},
			toSearch(val) {
				this.param.page = 1
				this.param.title = val
				this.getList()
			},
			async getList() {
				let {
					list: oldList,
					param
				} = this
				let {
					id
				} = this.options
				param.cate_id = id
				let newList = await this.$api.service.serviceList(param);

				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			
			getCity(){
				var that = this
				uni.request({
					url:that.site_root+'/index/getCity',
					method:'GET',
					success(result) {
						that.cityList = result.data.data
					},
				})
			},
			pickerChange(e, val) {
				let ind = e.target.value
				this.cityIndex = ind
				this.cityId = this.cityList[ind].id
				this.param.cityId = this.cityId
				this.getList()
			},
		}
	}
</script>


<style lang="scss">
	page{
		background-color: rgb(230, 230, 230);
	}
</style>